/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.bean.impl;

import java.util.Arrays;

import org.springframework.util.ObjectUtils;

import com.genia.toolbox.basics.bean.EqualityMethod;
import com.genia.toolbox.basics.bean.MutablePair;

/**
 * basic implementation for {@link MutablePair}.
 * 
 * @param <T1>
 *          the type of the first element of the pair
 * @param <T2>
 *          the type of the second element of the pair
 */
public abstract class AbstractPair<T1, T2>
    implements MutablePair<T1, T2>
{

  /**
   * the {@link EqualityMethod} to use.
   */
  protected transient final EqualityMethod equalityMethod;

  /**
   * the first element of the pair.
   */
  private T1 firstElement;

  /**
   * the second element of the pair.
   */
  private T2 secondElement;



  /**
   * constructor.
   * 
   * @param equalityMethod
   *          the {@link EqualityMethod} to use
   */
  protected AbstractPair(EqualityMethod equalityMethod)
  {
    this.equalityMethod = equalityMethod;
  }



  /**
   * constructor.
   * 
   * @param equalityMethod
   *          the {@link EqualityMethod} to use
   * @param firstElement
   *          the first element of the pair.
   * @param secondElement
   *          the second element of the pair.
   */
  protected AbstractPair(EqualityMethod equalityMethod, T1 firstElement, T2 secondElement)
  {
    this.equalityMethod = equalityMethod;
    this.firstElement = firstElement;
    this.secondElement = secondElement;
  }



  /**
   * Indicates whether some other object is "equal to" this one.
   * 
   * @param obj
   *          the reference object with which to compare.
   * @return <code>true</code> if this object is the same as the obj argument;
   *         <code>false</code> otherwise.
   * @see #hashCode()
   * @see java.util.Hashtable
   */
  @Override
  public boolean equals(final Object obj)
  {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (obj instanceof AbstractPair) {
      AbstractPair<?, ?> other = (AbstractPair<?, ?>) obj;
      return equalityMethod.isEqual(getFirstElement(), other.getFirstElement()) && equalityMethod.isEqual(getSecondElement(), other.getSecondElement());
    }
    return false;
  }



  /**
   * getter for the firstElement property.
   * 
   * @return the firstElement
   */
  public T1 getFirstElement()
  {
    return firstElement;
  }



  /**
   * getter for the secondElement property.
   * 
   * @return the secondElement
   */
  public T2 getSecondElement()
  {
    return secondElement;
  }



  /**
   * Returns a hash code value for the object. This method is supported for the
   * benefit of hashtables such as those provided by {@link java.util.Hashtable}.
   * 
   * @return a hash code value for this object.
   * @see java.lang.Object#equals(java.lang.Object)
   * @see java.util.Hashtable
   */
  @Override
  public int hashCode()
  {
    return ObjectUtils.nullSafeHashCode(Arrays.asList(equalityMethod.getHash(getFirstElement()), equalityMethod.getHash(getSecondElement())));
  }



  /**
   * setter for the firstElement property.
   * 
   * @param firstElement
   *          the firstElement to set
   */
  public void setFirstElement(T1 firstElement)
  {
    this.firstElement = firstElement;
  }



  /**
   * setter for the secondElement property.
   * 
   * @param secondElement
   *          the secondElement to set
   */
  public void setSecondElement(T2 secondElement)
  {
    this.secondElement = secondElement;
  }
}
